﻿m.service("$wslang", [function () {
    return {
       
        $getUrl:function(){
            return ngW8.$("$store").$get("$SERVICE_URL")+"/Language.svc";
        },
        $loadByFormName: function (LanguageCode, FormName, loadComplete) {
            var me = this;

            $.ajax({
                type: "POST",
                url: me.$getUrl() + "/list/" + LanguageCode,
                data: JSON.stringify({
                    FormName: FormName
                }),
                success: function (res) {

                    loadComplete(res);

                },
                error: function (error) {
                  
                    var div = $("<div><iframe></iframe></div>").appendTo("body");
                    div.kendoWindow({
                        width: $(window).width() - 40,
                        height: $(window).height() - 40
                    });
                    div.data("kendoWindow").center()
                    var iframe = div.children()[0];
                    $(iframe).css("width", $(window).width() - 40);
                    $(iframe).css("height", $(window).height() - 60);

                    var safeData = window.toStaticHTML(error.responseText);
                    iframe.contentWindow.document.open();
                    iframe.contentWindow.document.write(safeData);
                    iframe.contentWindow.document.close();
                   
                },
                processData: true,
                dataType: "json",
                contentType: "application/json; charset=utf-8"
            });

        },

        $apply: function (scope,complete) {
            var me = this;
            if (!window.top.$$$lang) {
                window.top = {};
            }
            var scopeKey = scope.$$$templatePath.replace(":", "_");
            while (scopeKey.indexOf('/') > -1) {
                scopeKey = scopeKey.replace('/', '$');
            }
            scope
            if (!window.top.$$$lang[scopeKey]) {
                window.top.$$$lang[scopeKey] = {};
            }
            scope.$$$lang = window.top.$$$lang[scopeKey];
            if (!scope.$$$lang[ngW8.$("$lang").$getLanguageCode()]) {
                scope.$$$lang[ngW8.$("$lang").$getLanguageCode()] = {};
                me.$loadByFormName(ngW8.$("$lang").$getLanguageCode(), scope.$$$templatePath, function (res) {
                    angular.forEach(res, function (f) {
                        scope.$$$lang[ngW8.$("$lang").$getLanguageCode()][f[0]] = f[1];
                    });
                    scope.$$$res = function (Key, Caption, Type) {
                        if (!Caption) {
                            Caption = Key;
                            Type = "caption";
                        }
                        if (!Type) {
                            Type = "caption";
                        }

                        if (!scope.$$$lang) {
                            scope.$$$lang = {};
                        }
                        if (!scope.$$$lang[ngW8.$("$lang").$getLanguageCode()]) {
                            scope.$$$lang[ngW8.$("$lang").$getLanguageCode()] = {};
                        }
                        if (!scope.$$$lang[ngW8.$("$lang").$getLanguageCode()][Key]) {
                            $.ajax({
                                type: "POST",
                                url: me.$getUrl() + "/get/" + ngW8.$("$lang").$ins().code.split('-')[0],
                                data: JSON.stringify({
                                    FormName: scope.$$$templatePath,
                                    Key: Key,
                                    Caption: Caption,
                                    LabelType:Type
                                }),
                                success: function (res) {

                                    scope.$$$lang[ngW8.$("$lang").$getLanguageCode()][Key] = res;

                                },
                                error: function (error) {
                                }
                            });
                            scope.$$$lang[ngW8.$("$lang").$getLanguageCode()][Key] = Caption;
                        }
                        return scope.$$$lang[ngW8.$("$lang").$getLanguageCode()][Key];
                    };

                    if (complete) {
                        complete(scope);
                    }
                    else {
                        scope.$apply();
                    }
                });
            }
            else {
                if (complete) {
                    complete(scope);
                }
                else {
                    scope.$apply();
                }
            }
       
           
          
        },
        $synchronize: function (Page, KeyType,Key, Caption) {

        }
    }
}]);
